﻿@{
    ViewBag.Title = "ProphaseFileList";
    Layout = "~/Views/Shared/_UserPage.cshtml";
}
<script type="text/javascript">
    $(function () {
        $("#ProjectID").combobox({
            editable: false, panelHeight: "auto",
            valueField: 'ID',
            textField: 'ProjectName',
            url: '/ProjectInfo/ProjectInfoList',
            onLoadSuccess: function () {
                var data = $("#ProjectID").combobox('getData');
                if (data.length > 0) {
                    $("#ProjectID").combobox('select', data[0]["ID"]);
                }
                else {
                    initTable('');
                }
            },
            onSelect: function (record) {
                ProphaseFileList.initTable(record.ID);
            }
        });
    });

    var ProphaseFileList = {
        initTable: function (projectID) {
            $('#prophaseFileDataGrid').datagrid({
                url: '/ProphaseFileInfo/GetProphaseFileList?pid=' + projectID,//一个用以从远程站点请求数据的超链接地址
                iconCls: 'icon-bricks',
                nowrap: true,//当数据长度超出列宽时将会自动截取
                autoRowHeight: false,
                striped: true,//设置为true将交替显示行背景
                collapsible: true,
                pagination: true,
                pageSize: 20,
                rownumbers: true,
                remoteSort: false,
                idField: 'ID',//表明该列是一个唯一列。
                singleSelect: true,
                fitColumns: true,
                toolbar: '#prophaseFileToolBar',
                columns: [[
                            { title: 'ID', field: 'ID', hidden: true },
                            { title: 'ProjectStageID', field: 'ProjectStageID', hidden: true },
                            { title: 'ProjectStageDealPerson', field: 'ProjectStageDealPerson', hidden: true },
                            { title: 'DealPerson', field: 'DealPerson', hidden: true },
                            { title: '阶段名称', field: 'ProjectStageName', width: 150, sortable: true },
                            { title: '文件名称', field: 'FileName', width: 200, sortable: true },
                            { title: '编制状况', field: 'PreparationStatus', width: 100, sortable: true },
                            { title: '计划完成时间', field: 'PlannedCompletionTime', width: 100, sortable: true },
                            { title: '批准文号', field: 'ApprovalNumber', width: 300, sortable: true },
                            { title: '批复时间', field: 'ApprovalTime', width: 100, sortable: true },
                            { title: '办理人', field: 'DealPersonName', width: 100, sortable: true },
                            { title: '办理部门', field: 'DealPersonOrgName', width: 100, sortable: true },
                            { title: '附件', field: 'Attachment', width: 500, sortable: true },
                            //{
                            //    title: '附件', field: 'Attachment', width: 300, sortable: true,
                            //    formatter: function (value, row, index) {
                            //        return "<a href='javascript:void(0)' class='easyui-linkbutton' onclick='$.Upload(\"" + row.ID + "\", \"ProphaseManagement/ProphaseFileList/Attachment\",\"true\")' data-options='plain:true,iconCls:\"icon-blue_lego\"'>附件操作</a>";
                            //    }
                            //}
                ]],
                onLoadSuccess: function (data) {
                    if (data.rows.length > 1) {
                        mergeCellsByField("prophaseFileDataGrid", "ProjectStageName");
                    }
                },
                onClickRow: function (rowIndex, rowData) {
                    if (rowData.ProjectStageDealPerson != rowData.LoginUserID) {
                        $('#btnModifyStage').linkbutton('disable');
                        $('#btnDeleteStage').linkbutton('disable');
                        $('#btnUpStage').linkbutton('disable');
                        $('#btnDownStage').linkbutton('disable');
                    }
                    else {
                        $('#btnModifyStage').linkbutton('enable');
                        $('#btnDeleteStage').linkbutton('enable');
                        $('#btnUpStage').linkbutton('enable');
                        $('#btnDownStage').linkbutton('enable');
                        if (rowData.ID != "00000000-0000-0000-0000-000000000000") {
                            $('#btnDeleteStage').linkbutton('disable');
                        }
                    }

                    if (rowData.DealPerson != rowData.LoginUserID) {
                        $('#btnModifyFile').linkbutton('disable');
                        $('#btnDeleteFile').linkbutton('disable');
                        $('#btnUpFile').linkbutton('disable');
                        $('#btnDownFile').linkbutton('disable');
                    }
                    else {
                        $('#btnModifyFile').linkbutton('enable');
                        $('#btnDeleteFile').linkbutton('enable');
                        $('#btnUpFile').linkbutton('enable');
                        $('#btnDownFile').linkbutton('enable');
                        if (rowData.ID == "00000000-0000-0000-0000-000000000000") {
                            $('#btnDeleteStage').linkbutton('disable');
                            $('#btnUpFile').linkbutton('disable');
                            $('#btnDownFile').linkbutton('disable');
                        }
                    }
                },
                onDblClickRow: function (rowIndex, rowData) {
                    ProphaseFileList.FilePreview(rowData);
                }
            });
        },

        //新增阶段
        AddStage: function () {
            var data = $("#ProjectID").combobox('getData');
            if (data.length > 0) {
                var projectID = $("#ProjectID").combobox('getValue');
                ProphaseFileList.StageMutual("新增", "/ProphaseFileInfo/GetProjectStageInfo?pid=" + projectID);
            }
            else { $.EasyuiSlide("还没有项目，不能添加"); }
        },

        //修改阶段
        ModifyStage: function () {
            var row = $("#prophaseFileDataGrid").datagrid("getSelected");
            if (row) {
                ProphaseFileList.StageMutual("修改", "/ProphaseFileInfo/GetProjectStageInfo?id=" + row.ProjectStageID);
            }
            else { $.EasyuiSlide("请选择一条记录"); }
        },

        //弹出阶段编辑窗口
        StageMutual: function (title, href) {
            $.ModelDialog({
                title: title, href: href, width: 300,
                handler: function () {
                    $.ModelForm({
                        validate: true, button: $(this),
                        id: "#stage-model",
                        url: "/ProphaseFileInfo/SaveProjectStageInfo",
                        success: function (result) { ProphaseFileList.Reload(); }
                    });
                },
                onLoad: function () { }
            });
        },

        //删除阶段
        DeleteStage: function () {
            var arr = $('#prophaseFileDataGrid').datagrid('getSelections');
            if (arr.length == 1) {
                $.messager.confirm('操作提示', "确认删除阶段[ " + arr[0].ProjectStageName + " ]吗？", function (r) {
                    if (r) {
                        $.post("/ProphaseFileInfo/DeleteProjectStageInfo?id=" + arr[0].ProjectStageID, { query: arr.join(",") }, function (result) {
                            if (result.ResultType == 0) {  //0表示成功
                                $.EasyuiSlide(result.Message);
                                ProphaseFileList.Reload();
                            }
                            else {
                                $.EasyuiSlide(result.Message);
                            }
                        });
                    }
                });

            } else {
                $.EasyuiSlide("请选择一条数据");
            }
            return false;
        },

        //向上
        UpStage: function () {
            ProphaseFileList.OrderStage("Up");
        },

        //向下
        DownStage: function () {
            ProphaseFileList.OrderStage("Down");
        },

        //向上或向下方法
        OrderStage: function (order) {
            var row = $("#prophaseFileDataGrid").datagrid("getSelected");
            if (row) {
                //id：识别当前记录；pid：识别涉及调整的记录集合；order：向上或向下
                $.post("/ProphaseFileInfo/SetStageOrder", { id: row.ProjectStageID, order: order }, function (result) {
                    if (result.ResultType == 0) {
                        $.EasyuiSlide(result.Message);  //成功则右下角提示
                        ProphaseFileList.Reload();
                        //$("#myTreeGrid").treegrid("selectRecord", row.ID);  //重新选中对应ID的记录（此处不生效，原因不明，后续探究）
                    }
                    else {
                        $.EasyuiMsg(result.Message);  //失败则弹出框提示
                    }
                });
            }
            else { $.EasyuiSlide("请选择一条数据"); }
        },

        //添加文件
        AddFile: function () {
            var dataProject = $("#ProjectID").combobox('getData');
            var data = $("#prophaseFileDataGrid").datagrid('getData').rows;
            if (dataProject.length > 0 && data.length > 0) {
                var projectID = $("#ProjectID").combobox('getValue');
                var projectStageID = '';
                var row = $("#prophaseFileDataGrid").datagrid("getSelected");
                if (row) {
                    projectStageID = row.ProjectStageID;
                }
                ProphaseFileList.FileMutual("新增", "/ProphaseFileInfo/GetProphaseFileInfo?pid=" + projectID + "&projectStageID=" + projectStageID, true);
            }
            else { $.EasyuiSlide("必须先有项目和阶段"); }
        },

        //修改文件
        ModifyFile: function () {
            var row = $("#prophaseFileDataGrid").datagrid("getSelected");
            if (row) {
                ProphaseFileList.FileMutual("修改", "/ProphaseFileInfo/GetProphaseFileInfo?id=" + row.ID);
            }
            else { $.EasyuiSlide("请选择一条记录"); }
        },

        //弹出文件编辑窗口
        FileMutual: function (title, href, isAdd) {
            $.ModelDialog({
                title: title, href: href, width: 800,
                handler: function () {
                    $("#ApprovalNumber").attr("disabled", false);
                    $('#modifyInfoDataGrid').datagrid('acceptChanges');
                    var lstModifyInfo = $('#modifyInfoDataGrid').datagrid('getData').rows;
                    var JsonProphaseFileModifyInfo = JSON.stringify(lstModifyInfo);
                    $('#hJsonProphaseFileModifyInfo').val(JsonProphaseFileModifyInfo);

                    $.ModelForm({
                        validate: true, button: $(this),
                        id: "#file-model",
                        url: "/ProphaseFileInfo/SaveProphaseFileInfo",
                        success: function (result) { ProphaseFileList.Reload(); }
                    });
                },
                onLoad: function () { ProphaseFileList.FileFormInit(isAdd); }
            });
        },

        //弹窗初始化
        FileFormInit: function (isAdd) {
            //阶段（下拉）
            var projectID = $("#ProjectID").combobox('getValue');
            $("#ProjectStageID").combobox({
                url: "/ProphaseFileInfo/GetProjectStageInfoList?pid=" + projectID,
                editable: false, panelHeight: "auto",
                valueField: "ID",
                textField: "StageName",
                //如果是新增，则让下拉框默认选中第一个
                onLoadSuccess: function () {
                    if (isAdd) {
                        var data = $("#ProjectStageID").combobox('getData');
                        var projectStageID = $("#ProjectStageID").combobox('getValue');
                        if (data.length > 0 && projectStageID == '') {
                            $("#ProjectStageID").combobox('select', data[0]["ID"]);
                        }
                    }
                }
            });

            //发布单位（下拉）--因为一个用户可能属于多个单位
            $("#DealPersonOrg").combobox({
                url: "/Account/GetUserOrgsByUserID?userID=" + $("#hDealPerson").val(),
                editable: false, width: 155, panelHeight: "auto",
                valueField: "ID",
                textField: "OrgName",
                //如果是新增，则让下拉框默认选中第一个
                onLoadSuccess: function () {
                    if (isAdd) {
                        var data = $("#DealPersonOrg").combobox('getData');
                        if (data.length > 0) {
                            $("#DealPersonOrg").combobox('select', data[0]["ID"]);
                        }
                    }
                }
            });
        },

        //删除
        DeleteFile: function () {
            var arr = $('#prophaseFileDataGrid').datagrid('getSelections');

            if (arr.length == 1) {
                $.messager.confirm('操作提示', "确认删除文件[ " + arr[0].FileName + " ]吗？", function (r) {
                    if (r) {
                        $.post("/ProphaseFileInfo/DeleteProphaseFileInfo?ID=" + arr[0].ID, { query: arr.join(",") }, function (result) {
                            if (result.ResultType == 0) {  //0表示成功
                                $.EasyuiSlide(result.Message);
                                ProphaseFileList.Reload();
                            }
                            else {
                                $.EasyuiSlide(result.Message);
                            }
                        });
                    }
                });

            } else {
                $.EasyuiSlide("请选择一条数据");
            }
            return false;
        },

        //向上
        UpFile: function () {
            ProphaseFileList.OrderFile("Up");
        },

        //向下
        DownFile: function () {
            ProphaseFileList.OrderFile("Down");
        },

        //向上或向下方法
        OrderFile: function (order) {
            var row = $("#prophaseFileDataGrid").datagrid("getSelected");
            if (row) {
                //id：识别当前记录；pid：识别涉及调整的记录集合；order：向上或向下
                $.post("/ProphaseFileInfo/SetFileOrder", { id: row.ID, projectStageID: row.ProjectStageID, order: order }, function (result) {
                    if (result.ResultType == 0) {
                        $.EasyuiSlide(result.Message);  //成功则右下角提示
                        ProphaseFileList.Reload();
                    }
                    else {
                        $.EasyuiMsg(result.Message);  //失败则弹出框提示
                    }
                });
            }
            else { $.EasyuiSlide("请选择一条数据"); }
        },

        //预览
        FilePreview: function (rowData) {
            if (rowData.ID != "00000000-0000-0000-0000-000000000000") {
                ProphaseFileList.FilePreviewMutual("预览", "/ProphaseFileInfo/PreviewProphaseFileInfo?id=" + rowData.ID);
            }
            else {
                $.EasyuiMsg("阶段下还没有文件信息，不能预览");
            }
        },

        //实体操作
        FilePreviewMutual: function (title, href) {
            $.ModelDialog({
                title: title, href: href, width: 800,
                onLoad: function () { }
            });
        },

        //刷新
        Reload: function () {
            $("#prophaseFileDataGrid").datagrid("reload");
        }
    }
</script>

<div class="easyui-layout" fit="true">
    <div data-options="region:'north'" style="height:35px;">
        <table style="margin-top: 5px">
            <tr>
                <td style="width: 95px; text-align: center">项目名称</td>
                <td>
                    <input id="ProjectID" name="ProjectID" class="easyui-combobox" style="width: 180px;" />
                </td>
            </tr>
        </table>
    </div>

    <div data-options="region:'center'">
        <table id="prophaseFileDataGrid" fit="true"></table>
        <div id="prophaseFileToolBar">
            @Html.Raw(ViewBag.ToolBarInfo)
        </div>
    </div>
</div>